#include "widget.h"
#include "ui_widget.h"
#include<QLabel>
#include<QVBoxLayout>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->treeWidget->setHeaderLabel("动物");
    //新增顶层节点
    QTreeWidgetItem* item=new QTreeWidgetItem();
    item->setText(0,"猫");
    ui->treeWidget->addTopLevelItem(item);

    QTreeWidgetItem* item2=new QTreeWidgetItem();
    item2->setText(0,"狗");
    ui->treeWidget->addTopLevelItem(item2);

    QTreeWidgetItem* item3=new QTreeWidgetItem();
    item3->setText(0,"鸟");
    ui->treeWidget->addTopLevelItem(item3);
    //新增普通节点
    QTreeWidgetItem* item4=new QTreeWidgetItem();
    item4->setText(0,"美短");
    item->addChild(item4);

    QTreeWidgetItem* item5=new QTreeWidgetItem();
    item5->setText(0,"布偶");
    item->addChild(item5);

    QTreeWidgetItem* item6=new QTreeWidgetItem();
    item6->setText(0,"中华田园犬");
    item2->addChild(item6);

    QTreeWidgetItem* item7=new QTreeWidgetItem();
    item7->setText(0,"柯基");
    item2->addChild(item7);

    QLabel* label1=new QLabel(ui->tab);
    label1->setText("标签页1");
    label1->resize(100,50);

    QLabel* label2=new QLabel(ui->tab_2);
    label2->setText("标签页2");
    label2->resize(100,50);

    QPushButton* button1=new QPushButton("按钮1");
    QPushButton* button2=new QPushButton("按钮2");
    QPushButton* button3=new QPushButton("按钮3");

    QVBoxLayout* layout=new QVBoxLayout();
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);

    this->setLayout(layout);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_addtop_clicked()
{
    const QString& text=ui->lineEdit->text();
    QTreeWidgetItem*item=new QTreeWidgetItem();
    item->setText(0,text);
    ui->treeWidget->addTopLevelItem(item);
}

void Widget::on_pushButton_addchoose_clicked()
{
    QTreeWidgetItem*column=ui->treeWidget->currentItem();
    if(column==nullptr) {
        return;
    }
    const QString& text=ui->lineEdit->text();
    QTreeWidgetItem*item=new QTreeWidgetItem();
    item->setText(0,text);
    column->addChild(item);

}

void Widget::on_pushButton_deletechoose_clicked()
{
    QTreeWidgetItem*current=ui->treeWidget->currentItem();
    if(current->parent()==nullptr){
        int index=ui->treeWidget->indexOfTopLevelItem(current);
        ui->treeWidget->takeTopLevelItem(index);
    }
    else
    {
        current->parent()->removeChild(current);
    }

}

void Widget::on_pushButton_clicked()
{
    QWidget*w=new QWidget();
    int count=ui->tabWidget->count();
    ui->tabWidget->addTab(w,QString("Tab ")+QString::number(count+1));
    QLabel*laber=new QLabel(w);
    laber->setText(QString("标签页")+QString::number(count+1));
    ui->tabWidget->setCurrentIndex(count);
}

void Widget::on_pushButton_2_clicked()
{
    int index=ui->tabWidget->currentIndex();
    ui->tabWidget->removeTab(index);
}

void Widget::on_tabWidget_tabCloseRequested(int index)
{
    ui->tabWidget->removeTab(index);
}
